import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:live_music/components/ui/theme.dart';
import 'package:live_music/controller/userInfo_controller.dart';
import 'package:live_music/utils/util.dart';

class PrivacySetting extends StatefulWidget {
  PrivacySetting({Key key}) : super(key: key);

  @override
  _PrivacySettingState createState() => _PrivacySettingState();
}

class _PrivacySettingState extends State<PrivacySetting>
    with SingleTickerProviderStateMixin {
  final _userInfoController = Get.find<UserInfoController>();
  bool isShowMoment;
  bool isShowCollected;
  bool isShowTopic;
  bool isShowLiked;
  bool isShowFriend;
  @override
  void initState() {
    super.initState();
    setState(() {
      isShowMoment = _userInfoController.userSetting.value.isShowMoment != 0
          ? true
          : false;
      isShowCollected =
          _userInfoController.userSetting.value.isShowCollected != 0
              ? true
              : false;
      isShowTopic =
          _userInfoController.userSetting.value.isShowTopic != 0 ? true : false;
      isShowLiked =
          _userInfoController.userSetting.value.isShowLiked != 0 ? true : false;
      isShowFriend = _userInfoController.userSetting.value.isShowFriend != 0
          ? true
          : false;
    });
  }

  _updateUserInfo() async {
    Map<String, dynamic> settingData = {};
    settingData["isShowMoment"] = isShowMoment == true ? 1 : 0;
    settingData["isShowCollected"] = isShowCollected == true ? 1 : 0;
    settingData["isShowTopic"] = isShowTopic == true ? 1 : 0;
    settingData["isShowLiked"] = isShowLiked == true ? 1 : 0;
    settingData["isShowFriend"] = isShowFriend == true ? 1 : 0;
    _userInfoController.updateUserSetting(settingData);
    Get.back();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PreferredSize(
            preferredSize: Size.fromHeight(50),
            child: AppBar(
              elevation: 0.0,
              centerTitle: true,
              title: Center(
                child: Container(
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      GestureDetector(
                        child: Text(
                          "取消",
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.grey,
                              fontWeight: FontWeight.w500),
                        ),
                        onTap: () {
                          Get.back();
                        },
                      ),
                      Container(
                        child: Text(
                          "隐私设置",
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.black,
                              fontWeight: FontWeight.w500),
                        ),
                      ),
                      InkWell(
                        child: Container(
                          child: Text(
                            "保存",
                            style: TextStyle(
                                fontSize: 16,
                                color: Colors.redAccent,
                                fontWeight: FontWeight.w500),
                          ),
                        ),
                        onTap: _updateUserInfo,
                      )
                    ],
                  ),
                ),
              ),
              automaticallyImplyLeading: false,
            )),
        body: Column(children: [
          _showFriend(),
          _showMoment(),
          _showTopic(),
          _showCollected(),
          _showLiked()
        ]));
  }

  _showFriend() {
    return CellSetting('允许查看我的好友',
        onTap: () {},
        showRight: false,
        right: Container(
          height: 20,
          child: Switch(
              activeColor: Colors.redAccent,
              value: isShowFriend,
              onChanged: (value) {
                setState(() {
                  isShowFriend = value;
                });
              }),
        ));
  }

  _showMoment() {
    return CellSetting('允许查看我的笔记',
        onTap: () {},
        showRight: false,
        right: Container(
          height: 20,
          child: Switch(
              activeColor: Colors.redAccent,
              value: isShowMoment,
              onChanged: (value) {
                setState(() {
                  isShowMoment = value;
                });
              }),
        ));
  }

  _showTopic() {
    return CellSetting('允许查看我的话题',
        onTap: () {},
        showRight: false,
        right: Container(
          height: 20,
          child: Switch(
              activeColor: Colors.redAccent,
              value: isShowTopic,
              onChanged: (value) {
                setState(() {
                  isShowTopic = value;
                });
              }),
        ));
  }

  _showCollected() {
    return CellSetting('允许查看我的收藏',
        onTap: () {},
        showRight: false,
        right: Container(
          height: 20,
          child: Switch(
              activeColor: Colors.redAccent,
              value: isShowCollected,
              onChanged: (value) {
                setState(() {
                  isShowCollected = value;
                });
              }),
        ));
  }

  _showLiked() {
    return CellSetting('允许查看我的点赞',
        onTap: () {},
        showRight: false,
        right: Container(
          height: 20,
          child: Switch(
              activeColor: Colors.redAccent,
              value: isShowLiked,
              onChanged: (value) {
                setState(() {
                  isShowLiked = value;
                });
              }),
        ));
  }
}

class CellSetting extends StatelessWidget {
  const CellSetting(
    this.text, {
    Key key,
    this.border = true,
    this.showRight = true,
    this.right,
    this.onTap,
  }) : super(key: key);

  /// 底部 border 是否显示
  final bool border;
  final bool showRight;

  /// 右侧文字
  final String text;

  /// 右侧内容
  final Widget right;

  /// 点击回调
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
      child: InkWell(
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(20)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                  width: toRpx(size: 350),
                  child: Text(
                    text,
                    style: TextStyle(
                      fontSize: WcaoTheme.fsL,
                      color: WcaoTheme.base,
                    ),
                  )),
              Row(children: [
                Container(
                    alignment: Alignment.centerRight,
                    width: toRpx(size: 250),
                    child: right),
                showRight
                    ? Container(
                        margin: EdgeInsets.only(left: 5),
                        child: Icon(
                          Icons.arrow_forward_ios,
                          size: WcaoTheme.fsXl,
                          color: WcaoTheme.secondary,
                        ),
                      )
                    : Container()
              ])
            ],
          ),
        ),
      ),
    );
  }
}
